package com.nlelpct.manage.model.vo;

import com.nlelpct.manage.enums.ResultEnum;
import lombok.Data;

import java.io.Serializable;

/**
 * @author :小马
 * @date : 2023-07-12
 */
@Data
public class ResponseResult<T> implements Serializable {
    /**
     * 错误码
     */
    private Integer status;
    /**
     * 返回数据
     */
    private T data;
    /**
     * 错误提示，用户可阅读
     */
    private String msg;

    /**
     * 将传入的 result 对象，转换成另外一个泛型结果的对象
     */
    public static <T> ResponseResult<T> error(ResponseResult<?> result) {
        return error(result.getStatus(), result.getMsg());
    }

    public static <T> ResponseResult<T> error(Integer status, String message) {
        ResponseResult<T> result = new ResponseResult<>();
        result.status = status;
        result.msg = message;
        return result;
    }

    public static <T> ResponseResult<T> error(ResultEnum resultEnum) {
        ResponseResult<T> result = new ResponseResult<>();
        result.status = resultEnum.getStatus();
        result.msg = resultEnum.getMsg();
        return result;
    }

    public static <T> ResponseResult<T> error(Integer errorCode) {
        return error(errorCode, "");
    }

    public static <T> ResponseResult<T> success(T data) {
        ResponseResult<T> result = new ResponseResult<>();
        result.status = 200;
        result.data = data;
        result.msg = "";
        return result;
    }

    public static <T> ResponseResult<T> success(String message) {
        ResponseResult<T> result = new ResponseResult<>();
        result.status = 200;
        result.data = null;
        result.msg = message;
        return result;
    }

    public static <T> ResponseResult<T> success(T data, String message) {
        ResponseResult<T> result = new ResponseResult<>();
        result.status = 200;
        result.data = data;
        result.msg = message;
        return result;
    }
}
